package com.dg11185.nearshop.activity.user;

import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

import android.os.Bundle;
import android.view.View;

import com.android.nearshop.R;
import com.baidu.location.BDLocation;
import com.dg11185.libs.ormlite.android.apptools.OpenHelperManager;
import com.dg11185.libs.ormlite.dao.Dao;
import com.dg11185.nearshop.activity.SubPageActivity;
import com.dg11185.nearshop.baidumap.MyMapLocation;
import com.dg11185.nearshop.baidumap.MyMapLocation.MyBDLocationListener;
import com.dg11185.nearshop.db.entity.Merchant;
import com.dg11185.nearshop.dbHelper.OrmLiteHelper;
import com.dg11185.nearshop.view.adapter.ShopBrowseAdapter;
import com.dg11185.nearshop.view.xlistview.XListView;

/**
 * 最近浏览商家，
 * @author：Daibo    
 * @time：2014-6-30 上午10:52:03
 */
public class BrowseRecordActivity extends SubPageActivity{

    /** 数据库 */
    private OrmLiteHelper databaseHelper = null;// 底包数据库helper

    private Dao<Merchant, Integer> merchantDao = null;
    
    private XListView listView = null;
    
    private List<Merchant> merchants = null;
    
    private ShopBrowseAdapter listAdapter = null;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_browse_record);
        setSubPageTitle("最近浏览商家");
        merchantDao = getHelper().getMerchantDao();
        listView = (XListView)this.findViewById(R.id.user_browse_record_list);
        listView.setPullLoadEnable(false);
        listView.setPullRefreshEnable(false);
        
        initList();
    }
    
    private void initList(){
        try {
            merchants = merchantDao.queryForAll();
            Collections.reverse(merchants);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(merchants.size()!=0){
            listAdapter = new ShopBrowseAdapter(this, merchants);
            listView.setAdapter(listAdapter);
            if(MyMapLocation.location!=null){
                listAdapter.setLocation(MyMapLocation.location);
            }else{
                initLocationData();
            }
            listAdapter.notifyDataSetChanged();
        }else{
            findViewById(R.id.user_browse_record_notice).setVisibility(View.VISIBLE);
        }
    }

    private void initLocationData() {

        MyMapLocation mapLocation = new MyMapLocation(this, new MyBDLocationListener() {
            
            @Override
            public void onReceiveLocation(BDLocation bDLocation) {
                listAdapter.setLocation(bDLocation);
                listAdapter.notifyDataSetChanged();}
        });
        mapLocation.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        /*
         * You'll need this in your class to release the helper when done.
         */
        if (databaseHelper != null) {
            OpenHelperManager.releaseHelper();
            databaseHelper = null;
        }
    }

    /**
     * You'll need this in your class to get the helper from the manager once
     * per class.
     */
    private OrmLiteHelper getHelper() {
        if (databaseHelper == null) {
            databaseHelper = OpenHelperManager.getHelper(this, OrmLiteHelper.class);
        }
        return databaseHelper;
    }

}
